<?php
//解析xml类
class Helper_ParserXml
{
   function ParserXml()
   {
    
   }
   function ParserXmlByUrl($xmlstr)
   {
     
   if ($xmlstr === false){
     return 0;
   }
   return self::GetXMLTree($xmlstr);
   }
   function GetXMLTree($xmldata)
   {
     ini_set ('track_errors', '1');
     $xmlreaderror = false;
     $parser = xml_parser_create();
     xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
     xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
     if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
        $xmlreaderror = true;
        return 0;
     }
     xml_parser_free($parser);

     if(!$xmlreaderror){
        $result = array ();
        $i = 0;
        if (isset($vals[$i]['attributes'])){
            foreach (array_keys ($vals [$i]['attributes']) as $attkey)
            $attributes[$attkey] = $vals[$i]['attributes'][$attkey];
     }    
        $result[$vals [$i]['tag']] = array_merge((array)$attributes, self::GetChildren($vals, $i, 'open'));
     }
     ini_set('track_errors', '0');
     return $result;
   }
   function GetChildren ($vals, &$i, $type)
   {
     if ($type == 'complete') {
        if (isset ($vals [$i]['value']))
            return ($vals [$i]['value']);
        else
            return '';
   }
   $children = array ();
   while ($vals [++$i]['type'] != 'close') {
        $type = $vals [$i]['type'];
        if (isset ($children [$vals [$i]['tag']])) {
            if (is_array ($children [$vals [$i]['tag']])) {
                $temp = array_keys ($children [$vals [$i]['tag']]);
                if (is_string ($temp [0])) {
                    $a = $children [$vals [$i]['tag']];
                    unset ($children [$vals [$i]['tag']]);
                    $children [$vals [$i]['tag']][0] = $a;
                }
            } else {
                $a = $children [$vals [$i]['tag']];
                unset ($children [$vals [$i]['tag']]);
                $children [$vals [$i]['tag']][0] = $a;
            }

            $children [$vals [$i]['tag']][] = self::GetChildren ($vals, $i, $type);
        } else
            $children [$vals [$i]['tag']] = self::GetChildren ($vals, $i, $type);
        if (isset ($vals [$i]['attributes'])) {
            $attributes = array ();
            foreach (array_keys ($vals [$i]['attributes']) as $attkey)
            $attributes [$attkey] = $vals [$i]['attributes'][$attkey];
            if (isset ($children [$vals [$i]['tag']])) {
                if ($children [$vals [$i]['tag']] == '') {
                    unset ($children [$vals [$i]['tag']]);
                    $children [$vals [$i]['tag']] = $attributes;
                }
                elseif (is_array ($children [$vals [$i]['tag']])) {
                    $index = count ($children [$vals [$i]['tag']]) - 1;
                    if ($children [$vals [$i]['tag']][$index] == '') {
                        unset ($children [$vals [$i]['tag']][$index]);
                        $children [$vals [$i]['tag']][$index] = $attributes;
                    }
                    $children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
                } else {
                    $value = $children [$vals [$i]['tag']];
                    unset ($children [$vals [$i]['tag']]);
                    $children [$vals [$i]['tag']]['value'] = $value;
                    $children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
                }
            } else
                $children [$vals [$i]['tag']] = $attributes;
        }
   }
     return $children;
   }
}
?>